from contextlib import asynccontextmanager

from fastapi import FastAPI

from .config.db import create_db_and_tables
from .schedule.task_schedule import start_schedule_tasks, close_schedule_tasks
from ..app.routers import task

@asynccontextmanager
async def lifespan(app: FastAPI):
    # 创建引擎
    await create_db_and_tables()
    # 开启调度任务
    await start_schedule_tasks()
    yield

    await close_schedule_tasks()


app = FastAPI(lifespan=lifespan)

# 挂载路由
app.include_router(task.router, prefix="/api/task")


@app.get("/health")
async def health_check():
    return {"status": "healthy"}
